// QuicksView.h : interface of the CQuicksView class
//
/////////////////////////////////////////////////////////////////////////////

#pragma once

#include "atlcrack.h"
#include "WindowManager.h"
#include "IdoComplete.h"
#include "SimpleLog.h"
using namespace quicks;

class CMainFrame;

#define ID_SHOW 66666

class CQuicksView : public CWindowImpl<CQuicksView>
{
public:
	DECLARE_WND_CLASS(NULL)

	BOOL PreTranslateMessage(MSG* pMsg);

	BEGIN_MSG_MAP(CQuicksView)
		MSG_WM_CREATE(OnCreate)
		MSG_WM_CHAR(OnChar)
		MSG_WM_KEYDOWN(OnKeyDown)
		MSG_WM_KEYUP(OnKeyUp)
		MSG_WM_DESTROY(OnDestroy)
		MSG_WM_HOTKEY(OnHotKey)
		MSG_WM_PAINT(OnPaint)
		MSG_WM_KILLFOCUS(OnKillFocus)
		//MSG_WM_ERASEBKGND(OnEraseBkgnd)
		MSG_WM_CTLCOLORSTATIC(OnCtlColorStatic)
		MESSAGE_HANDLER(ID_SHOW, OnShow)

	END_MSG_MAP()

// Handler prototypes (uncomment arguments if needed):
//	LRESULT MessageHandler(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
//	LRESULT CommandHandler(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
//	LRESULT NotifyHandler(int /*idCtrl*/, LPNMHDR /*pnmh*/, BOOL& /*bHandled*/)

	CQuicksView(CMainFrame* frame);
	int OnCreate(LPCREATESTRUCT lpCreateStruct);
	void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
	void OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags);
	void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
	void OnDestroy();
	void OnHotKey(int nHotKeyID, UINT uModifiers, UINT uVirtKey);
	void OnPaint(CDCHandle dc);
	void OnKillFocus(CWindow wndFocus);
	HBRUSH OnCtlColorStatic(CDCHandle dc, CStatic wndStatic);
	BOOL OnEraseBkgnd(CDCHandle dc);

	void ForwardDelete(int len);

private:
	void QueryWindows();
	void RefreshResults();
	void Clear();
	int DrawStringAndMeasure(CPaintDC& pdc, LPCTSTR str, int len, RECT& r);
	int DrawStringAndMeasure(CPaintDC& pdc, wstring str, RECT& r);
	WindowManager m_wm;
	int m_totalLength;
	int m_resultsLength;
	static const int MAXTEXT = 1024;
	wstring m_text;
	int m_popHeight;
	int m_maxLines;
	int m_textCaretPos;
	int m_textWidths[MAXTEXT];
	std::vector<WindowManager::WindowDesc> m_shownWindows;
	IdoComplete<WindowManager::WindowDesc> m_ic;
	IdoComplete<WindowManager::WindowDesc>::MatchResult m_matchResult;
	std::vector<MatchInput<WindowManager::WindowDesc> > m_windows;
	bool m_init;
	HFONT m_hfRegular;
	HFONT m_hfBold;
	long m_lfHeight;
	HFONT m_hf;
	CMainFrame* m_frame;
public:

	LRESULT OnNextMatch(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT OnPrevMatch(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT OnNextChar(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT OnPrevChar(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT OnNextWord(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT OnPrevWord(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT OnFirst(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT OnLast(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT OnKillLine(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT OnKillWord(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT OnKillWordForward(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT OnKillCharForward(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT OnRefresh(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
	LRESULT OnShow(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
};
